from google.appengine.ext import db
from datetime import date, datetime, timedelta
from horseanalysis.lib.callable import Callable

class BettingScheme(db.Model):
  name = db.StringProperty()
  expected_return_ranges = db.ListProperty(float)
  bets = db.ListProperty(int)
  
  def get_expected_bet(self, expected_return):
    min_expected_return = 0
    i = 0
    for max_expected_return in self.expected_return_ranges:
      if expected_return > min_expected_return and expected_return <= max_expected_return:
        return self.bets[i]
      i+=1
    return self.bets[i-1]
  
  @classmethod
  def get_scheme_by_name(cls, name):
    schemes = cls.gql("WHERE name = :1", name)
    for scheme in schemes:
      return scheme
    return None
  
  @classmethod
  def get_all_schemes(cls):
    return cls.all()
  
  @classmethod
  def create_scheme(cls, name, ranges, bets):
    if cls.gql("WHERE name = :1", name).count() == 0:      
      scheme = cls(name=name, expected_return_ranges=ranges, bets=bets)
      scheme.put()
  
  @classmethod
  def create_default_scheme(cls):
    cls.create_scheme('Flat', [1.0, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.5, 2.0, 3.0, 9999.0], [0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5])
    cls.create_scheme('Aggressive ', [1.0, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.5, 2.0, 3.0, 9999.0], [0, 5, 6, 7, 8, 10, 12, 15, 20, 30, 50])